#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>

const int N = 500005;
int n, k, l = 1, r = 1, ban[N], f[N], g[N], w[N], dep[N], fa[N], qu[N];
int e_cnt, heads[N];

inline int read() {
  int res = 0, ch = getchar();
  bool flag = false;
  while (!isdigit(ch)) {
    if (ch == '-') flag = true;
    ch = getchar();
  }
  while (isdigit(ch)) res = (res << 1) + (res << 3) + (ch ^ 48), ch = getchar();
  return flag ? -res : res;
}

struct Edge {
  int v, nxt, w;
} e[N << 1];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}

inline void bfs() {
  std::queue<int> q;
  q.push(1);
  memset(f, 0x3f, sizeof(f));
  f[0] = 0, fa[1] = 1;
  qu[r] = 0;
  dep[1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    g[u] = g[fa[u]] + w[u];
    if (!ban[u]) g[u] = std::min(g[u], f[dep[u]]);
    if (f[dep[u]] - k * dep[u] <= f[qu[r]] - k * qu[r]) qu[++r] = dep[u];
    for (int i = heads[u], v; i; i = e[i].nxt) {
      dep[v = e[i].v] = dep[u] + 1;
      f[dep[v]] = std::min(f[dep[v]], g[u] + w[v]);
      if (!ban[v]) {
        // for (int i = 0; i < dep[v]; ++i) {
        //   f[dep[v]] = std::min(f[dep[v]], f[i] + k * (dep[v] - i));
        // }
        f[dep[v]] = std::min(f[dep[v]], f[qu[r]] + k * (dep[v] - qu[r]));
      }
      q.push(v);
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("climb.in", "r", stdin);
  freopen("climb.out", "w", stdout);
#endif
#endif

  // scanf("%d%d", &n, &k);
  n = read(), k = read();
  for (int i = 2; i <= n; ++i) {
    // scanf("%d", &fa[i]);
    fa[i] = read();
    w[i] = read();
    ban[i] = read();
    // scanf("%d%d", &w[i], &ban[i]);
    add(fa[i], i, w[i]);
  }
  bfs();
  for (int i = 1; i <= n; ++i) {
    printf("%d\n", g[i]);
  }
  return 0;
}